package com.example.javaproject;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class IndividualSongs extends Activity {

	boolean isStop = true;
	ImageView start1;
	ImageView pause1;
	ImageView stop1;
	ImageView albumImage1;
	ImageView albumImage2;
	TextView albumNameTextView;
	TextView songsNameTextView;
	TextView singerNameTextView;
	boolean flag=true;

	// 进度条
	SeekBar seekBar = com.example.javaproject.MainActivity.seekBar;
	Timer timer = com.example.javaproject.MainActivity.timer;
	boolean isSeekBarChanging = com.example.javaproject.MainActivity.isSeekBarChanging;// 防止进度条与定时器冲突
	int currentPostion;// 当前播放进度
	MediaPlayer mediaPlayer = com.example.javaproject.MainActivity.albumMp;
	String pathString;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_individual_songs);
		albumImage1 = (ImageView) findViewById(R.id.tp1);
		albumImage2 = (ImageView) findViewById(R.id.tp3);
		albumNameTextView = (TextView) findViewById(R.id.te2);
		songsNameTextView = (TextView) findViewById(R.id.te3);
		singerNameTextView = (TextView) findViewById(R.id.te1);

		start1 = (ImageView) findViewById(R.id.start1);
		pause1 = (ImageView) findViewById(R.id.pause1);
		stop1 = (ImageView) findViewById(R.id.stop1);

		// MyListener listener = new MyListener();
		// start1.setOnClickListener(listener);
		// stop1.setOnClickListener(listener);
		// pause1.setOnClickListener(listener);

		String albumName = (String) getIntent().getSerializableExtra(
				"albumName");
		String songsName = (String) getIntent().getSerializableExtra(
				"songsName");
		String singerName = (String) getIntent().getSerializableExtra(
				"singerName");
		String songsPath = (String) getIntent().getSerializableExtra(
				"songsPath");
		pathString=songsPath;
		int currentPostion = (Integer) getIntent().getSerializableExtra(
				"currentPostion");

		boolean flag = (Boolean) getIntent().getSerializableExtra("flag");

		songsNameTextView.setText(songsName);
		singerNameTextView.setText(singerName);
		albumNameTextView.setText(albumName);
		if (albumName.equals("梦游计")) {
			albumImage1.setImageDrawable(getResources().getDrawable(
					R.drawable.album1));
			albumImage2.setImageDrawable(getResources().getDrawable(
					R.drawable.album1));
		} else if (albumName.equals("青年晚报")) {
			albumImage1.setImageDrawable(getResources().getDrawable(
					R.drawable.album2));
			albumImage2.setImageDrawable(getResources().getDrawable(
					R.drawable.album2));
		} else if (albumName.equals("苏格拉没有底")) {
			albumImage1.setImageDrawable(getResources().getDrawable(
					R.drawable.album3));
			albumImage2.setImageDrawable(getResources().getDrawable(
					R.drawable.album3));
		} else if (albumName.equals("尘")) {
			albumImage1.setImageDrawable(getResources().getDrawable(
					R.drawable.album4));
			albumImage2.setImageDrawable(getResources().getDrawable(
					R.drawable.album4));
		} else {
			albumImage1.setImageDrawable(getResources().getDrawable(
					R.drawable.album5));
			albumImage2.setImageDrawable(getResources().getDrawable(
					R.drawable.album6));
		}

		// 进度条
		// 监听滚动条事件
		seekBar = (SeekBar) findViewById(R.id.playSeekBar);
		seekBar.setOnSeekBarChangeListener(new MySeekBar());
		if (!flag) {
			mediaPlayer.seekTo(currentPostion);// 音乐从指定位置开始播放
			mediaPlayer.start();
		} else {
			mediaPlayer.pause();
		}

		seekBar.setMax(mediaPlayer.getDuration());// 设置拖动条最大值，而getDuration()方法需要在prepare后才能调用，否则返回-1

		// 监听播放时回调函数
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (!isSeekBarChanging) {
					seekBar.setProgress(mediaPlayer.getCurrentPosition());// 设置当前进度条的位置是音乐播放的位置
				}
			}
		}, 0, 50);

		// 播放事件
		start1.setOnClickListener(new PlayListener());
		// 暂停事件
		pause1.setOnClickListener(new PauseListener());
		// 停止事件
		stop1.setOnClickListener(new StopListener());

	}

	// 开始播放处理
	public class PlayListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			if (flag) {
				play();// 播放方法
			}else{
				mediaPlayer.reset();
				try {
					mediaPlayer.setDataSource(pathString);
					mediaPlayer.prepare();
					mediaPlayer.start();
					flag=true;
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
	}

	// 暂停播放处理
	public class PauseListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			mediaPlayer.pause();
		}
	}

	// 停止播放处理
	public class StopListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			mediaPlayer.stop();
			flag=false;
		}
	}

	// 播放处理方法
	public void play() {
		try {
			mediaPlayer.start();
			seekBar.setMax(mediaPlayer.getDuration());

			// 监听播放时回调函数
			timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					if (!isSeekBarChanging) {
						seekBar.setProgress(mediaPlayer.getCurrentPosition());
					}
				}
			}, 0, 50);
		} catch (Exception e) {
			Log.e("MainActivity", e.getMessage());
		}

	}

	// 进度条处理
	public class MySeekBar implements OnSeekBarChangeListener {

		@Override
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {

		}

		// 滚动时，应当暂停后台定时器
		@Override
		public void onStartTrackingTouch(SeekBar arg0) {
			isSeekBarChanging = true;
		}

		// 滑动结束后，重新设置值
		@Override
		public void onStopTrackingTouch(SeekBar arg0) {
			isSeekBarChanging = false;
			mediaPlayer.seekTo(seekBar.getProgress());
		}
	}

}
